//
//  FindTableViewCell.m
//  MountEverestFM
//
//  Created by ChenNing on 15/10/28.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "CommendListenTableViewCell.h"
#import "CommendListenCollectionViewCell.h"
#import "RecommendModel.h"

@interface CommendListenTableViewCell()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)UICollectionView *commCollectionView;
@end

@implementation CommendListenTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UICollectionViewFlowLayout *commFlowLayout = [[UICollectionViewFlowLayout alloc] init];
        commFlowLayout.itemSize = CGSizeMake((FM_Screen_Width-FM_Screen_Pts_Width*40)/3,(FM_Screen_Width-FM_Screen_Pts_Width*40)/3+30+FM_Screen_Pts_Height*12);
        commFlowLayout.sectionInset = UIEdgeInsetsMake(FM_Screen_Pts_Height*10, FM_Screen_Pts_Height*10, FM_Screen_Pts_Height*10, 0);
        commFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.commCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:commFlowLayout];
        self.commCollectionView.dataSource = self;
        self.commCollectionView.delegate = self;
        self.commCollectionView.showsHorizontalScrollIndicator = NO;
        self.commCollectionView.backgroundColor = [UIColor whiteColor];
        self.commCollectionView.scrollEnabled = NO;
        [self.contentView addSubview:_commCollectionView];
        
        [self.commCollectionView registerClass:[CommendListenCollectionViewCell class] forCellWithReuseIdentifier:@"commShowCell"];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.commCollectionView.frame = CGRectMake(0, FM_Screen_Pts_Height*10, FM_Screen_Width, (FM_Screen_Width-FM_Screen_Pts_Width*40)/3+FM_Screen_Pts_Height*12+30);
}

// UICollectionViewDataSource 代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[_recommendDic objectForKey:@"list"] count];
}

- (void)setRecommendDic:(NSDictionary *)recommendDic
{
    if (_recommendDic != recommendDic) {
        _recommendDic = recommendDic;
    }
    [self.commCollectionView reloadData];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CommendListenCollectionViewCell *commendListenCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"commShowCell" forIndexPath:indexPath];
    commendListenCell.commDictionary = [[self.recommendDic objectForKey:@"list"] objectAtIndex:indexPath.row];
    [commendListenCell.imageButton addTarget:self action:@selector(selectItemAction:) forControlEvents:UIControlEventTouchUpInside];
    commendListenCell.imageButton.tag = 10200+indexPath.row;
    return commendListenCell;
}

- (void)selectItemAction:(UIButton *)sender
{
    NSInteger index = sender.tag-10200;
    [self.delegate musicListTBPage:[[self.recommendDic objectForKey:@"list"] objectAtIndex:index]];
}

// UICollectionViewDelegate 代理方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate musicListTBPage:[[self.recommendDic objectForKey:@"list"] objectAtIndex:indexPath.row]];
}

@end
